package edu.upc.galaxy.controller;

import edu.upc.galaxy.entity.Credential;
import edu.upc.galaxy.entity.Cancha;
import edu.upc.galaxy.service.CanchaService;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.support.SessionStatus;
import org.springframework.web.servlet.ModelAndView;

/**
 *
 * @author Alvaro
 */
@Controller("canchaController")
public class CanchaController {

    @Autowired
    private CanchaService canchaService;
    private static Logger log = LoggerFactory.getLogger(CanchaController.class);

    @RequestMapping("/canchas/index")
    public ModelAndView index() {
        ModelAndView mav = new ModelAndView("canchas/index");
        List<Cancha> canchas = canchaService.buscarTodos();
        mav.addObject("canchas", canchas);
        return mav;
    }


    
    @RequestMapping("/canchas/buscar")
    public ModelAndView buscar() {
        ModelAndView mav = new ModelAndView("canchas/buscar");
        List<Cancha> canchas = canchaService.buscarTodos();
        mav.addObject("canchas", canchas);
        return mav;
    }

  /*  @RequestMapping("/canchas/alquilar")
    public ModelAndView buscar() {
        ModelAndView mav = new ModelAndView("canchas/alquilar");
        List<Cancha> canchas = canchaService.buscarCanchaDisponible();
        mav.addObject("canchas", canchas);
        return mav;
    }    
    */
    @RequestMapping(value = "/canchas/new", method = RequestMethod.GET)
    public ModelAndView newCancha() {
        ModelAndView mav = new ModelAndView("canchas/new");
        Cancha cancha = new Cancha();
        mav.getModelMap().put("cancha", cancha);
        return mav;
    }

    @RequestMapping(value = "/canchas/new", method = RequestMethod.POST)
    public String createCancha(@ModelAttribute("cancha")Cancha cancha, SessionStatus status) {    
        canchaService.insertar(cancha);
        status.setComplete();

        return "redirect:/pages/canchas/index";
        
    }
    
    @RequestMapping(value = "/canchas/edit", method = RequestMethod.GET)
    public ModelAndView editCancha(@RequestParam("id")Integer id) {    
        ModelAndView mav = new ModelAndView("canchas/edit");
        Cancha cancha = canchaService.buscar(id);
        mav.getModelMap().put("cancha", cancha);
        return mav;        
    }
    
    @RequestMapping(value="/canchas/edit", method=RequestMethod.POST)
    public String update(@ModelAttribute("cancha") Cancha cancha, SessionStatus status) {
        canchaService.actualizar(cancha);
        status.setComplete();
        return "redirect:/pages/canchas/index";
    }
    
    @RequestMapping("/canchas/delete")
    public ModelAndView delete(@RequestParam("id")Integer id)
    {
        ModelAndView mav = new ModelAndView("redirect:/pages/canchas/index");
        Cancha cancha = canchaService.buscar(id);
        canchaService.eliminar(cancha);
        return mav;
    }
    
    @RequestMapping("/canchas/login")
    public ModelAndView login() {
        ModelAndView mav = new ModelAndView("canchas/login");
        Credential credential = new Credential();
        mav.getModelMap().put("credential", credential);
        return mav;
    }    
    
    @RequestMapping(value = "/canchas/autenticar", method=RequestMethod.POST)
    public String autenticar(@ModelAttribute("credential") Credential credential,HttpServletRequest request) {
        if (canchaService.autenticar(credential.getCorreo(), credential.getPassword())) {
            request.getSession().setAttribute("credential",credential);
            return "redirect:/pages/canchas/index";
        }
        return "redirect:/pages/canchas/login";
    }
    
    @RequestMapping("/canchas/logout")
    public String logout(HttpServletRequest request) {
        request.getSession().invalidate();
        return "redirect:/pages/canchas/login";
    }        
    
    
}
